<?php

namespace App\Http\Validate\Kms\Home;

use App\Http\Validate\AbstractBaseValidate;
use App\Service\Kms\Home\HomeService;
use App\Service\Kms\Task\UserRoleService;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class HomeValidate extends AbstractBaseValidate
{
    function getRules(): array
    {
        // TODO: Implement getRules() method.
        return [];
    }

    function getAttributes(): array
    {
        // TODO: Implement getAttributes() method.
        return [
            "start"       => "开始日期",
            "end"         => "截止日期",
            "revenueType" => "营收类型",
            "type"        => "查询周期",
            "compareType" => "同比类型",
        ];
    }

    function registerValidateRules(): void
    {
        // TODO: Implement registerValidateRules() method.
    }

    function getMessages(): array
    {
        // TODO: Implement getMessages() method.
        return [];
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function activityStoreValidate(array $params)
    {
        $customerDateSelectionRuleFunc = function () use ($params) {
            switch ($params["type"] ?? 0) {
                case HomeService::ACTIVITY_STORE_DAU:
                case HomeService::ACTIVITY_STORE_WAU:
                    return "date_format:Y-m-d";
                    break;
                case HomeService::ACTIVITY_STORE_MAU:
                case HomeService::ACTIVITY_STORE_QAU:
                    return "date_format:Y-m";
                    break;
                case HomeService::ACTIVITY_STORE_YAU:
                    return "date_format:Y";
                    break;
            }
        };

        return self::getInstance()->validate(
            ["role", "regionSelectionType", "region", "regionValue", "type", "start", "end"],
            $params,
            [
                "role"                => [
                    "required",
                    "int",
                    Rule::in([
                        UserRoleService::ROLE_BASE_PROVINCE,
                        UserRoleService::ROLE_BASE_OBSERVER,
                        UserRoleService::ROLE_BASE_CITY_MANAGER,
                        UserRoleService::ROLE_BASE_DEVELOP_CITY_MANAGER,
                        UserRoleService::ROLE_BASE_SUPERVISOR,
                        UserRoleService::ROLE_SPECIAL_0,
                        UserRoleService::ROLE_SPECIAL_1,
                        UserRoleService::ROLE_SPECIAL_2,
                        UserRoleService::ROLE_BASE_WAR_REGION,
                    ])
                ],
                "regionSelectionType" => Rule::when(
                    ($params["role"] ?? null) == UserRoleService::ROLE_BASE_SUPERVISOR,
                    "nullable",
                    self::intInValidateBuild(true, HomeService::REGION_SELECTION_TYPE_TEXT)
                ),
                "region"              => Rule::when(
                    ($params["role"] ?? null) == UserRoleService::ROLE_BASE_SUPERVISOR,
                    "nullable",
                    "required|string|min:1|max:64"
                ),
                "regionValue"         => Rule::when(
                    ($params["role"] ?? null) == UserRoleService::ROLE_BASE_SUPERVISOR,
                    "nullable",
                    "required:string"
                ),
                "type"                => self::intInValidateBuild(true, HomeService::ACTIVITY_STORE_TEXT),
                "start"               => Rule::when(
                    !empty($params["start"]) || !empty($params["end"]),
                    array_filter([
                        "required",
                        $customerDateSelectionRuleFunc()
                    ])
                    , array_filter([
                        "nullable",
                        $customerDateSelectionRuleFunc()
                    ]
                )),
                "end"                 => Rule::when(
                    !empty($params["start"]) || !empty($params["end"]),
                    array_filter([
                        "required",
                        $customerDateSelectionRuleFunc(),
                        "after:start"
                    ])
                    , array_filter([
                        "nullable",
                        $customerDateSelectionRuleFunc(),
                        "after_or_equal:start"
                    ]
                ))
            ]
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function revenueSituationValidate(array $params)
    {
        return self::getInstance()->validate(
            ["region", "regionValue", "type", "compareType", "start", "end"],
            $params,
            [
                "role"                => [
                    "required",
                    "int",
                    Rule::in([
                        UserRoleService::ROLE_BASE_PROVINCE,
                        UserRoleService::ROLE_BASE_OBSERVER,
                        UserRoleService::ROLE_BASE_CITY_MANAGER,
                        UserRoleService::ROLE_BASE_DEVELOP_CITY_MANAGER,
                        UserRoleService::ROLE_BASE_SUPERVISOR,
                        UserRoleService::ROLE_SPECIAL_0,
                        UserRoleService::ROLE_SPECIAL_1,
                        UserRoleService::ROLE_SPECIAL_2,
                        UserRoleService::ROLE_BASE_WAR_REGION,
                    ])
                ],
                "regionSelectionType" => Rule::when(
                    ($params["role"] ?? null) == UserRoleService::ROLE_BASE_SUPERVISOR,
                    "nullable",
                    self::intInValidateBuild(true, HomeService::REGION_SELECTION_TYPE_TEXT)
                ),
                "region"              => Rule::when(
                    ($params["role"] ?? null) == UserRoleService::ROLE_BASE_SUPERVISOR,
                    "nullable",
                    "required|string|min:1|max:64"
                ),
                "regionValue"         => Rule::when(
                    ($params["role"] ?? null) == UserRoleService::ROLE_BASE_SUPERVISOR,
                    "nullable",
                    "required:string"
                ),
                "type"                => self::intInValidateBuild(true, HomeService::REVENUE_SITUATION_TYPE_TEXT),
                "compareType"         => self::intInValidateBuild(true,
                    HomeService::REVENUE_SITUATION_COMPARE_TYPE_TEXT),
                "start"               => [
                    "exclude_unless:type," . HomeService::REVENUE_SITUATION_TYPE_CUSTOM,
                    "required",
                    "date_format:Y-m-d"
                ],
                "end"                 => [
                    "exclude_unless:type," . HomeService::REVENUE_SITUATION_TYPE_CUSTOM,
                    "required",
                    "date_format:Y-m-d",
                    "after_or_equal:start"
                ]
            ],
            true,
            function ($params) {
                return array_filter($params, function ($val) {
                    if (strlen($val)) {
                        return true;
                    }
                    return false;
                });
            }
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function baseDataValidate(array $params)
    {
        return self::getInstance()->validate(
            ["role", "regionSelectionType", "region", "regionValue"],
            $params,
            [
                "role"                => [
                    "required",
                    "int",
                    Rule::in([
                        UserRoleService::ROLE_BASE_PROVINCE,
                        UserRoleService::ROLE_BASE_OBSERVER,
                        UserRoleService::ROLE_BASE_CITY_MANAGER,
                        UserRoleService::ROLE_BASE_DEVELOP_CITY_MANAGER,
                        UserRoleService::ROLE_BASE_SUPERVISOR,
                        UserRoleService::ROLE_SPECIAL_0,
                        UserRoleService::ROLE_SPECIAL_1,
                        UserRoleService::ROLE_SPECIAL_2,
                        UserRoleService::ROLE_BASE_WAR_REGION,
                    ])
                ],
                "regionSelectionType" => Rule::when(
                    ($params["role"] ?? null) == UserRoleService::ROLE_BASE_SUPERVISOR,
                    "nullable",
                    self::intInValidateBuild(true, HomeService::REGION_SELECTION_TYPE_TEXT)
                ),
                "region"              => Rule::when(
                    ($params["role"] ?? null) == UserRoleService::ROLE_BASE_SUPERVISOR,
                    "nullable",
                    "required|string|min:1|max:64"
                ),
                "regionValue"         => Rule::when(
                    ($params["role"] ?? null) == UserRoleService::ROLE_BASE_SUPERVISOR,
                    "nullable",
                    "required:string"
                )
            ]
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function newlyOpenedStoreValidate(array $params)
    {
        return self::getInstance()->validate(
            ["date", "role", "regionSelectionType", "region", "regionValue"],
            $params,
            [
                "date"                => "required||date_format:Y-m",
                "role"                => [
                    "required",
                    "int",
                    Rule::in([
                        UserRoleService::ROLE_BASE_PROVINCE,
                        UserRoleService::ROLE_BASE_OBSERVER,
                        UserRoleService::ROLE_BASE_CITY_MANAGER,
                        UserRoleService::ROLE_BASE_DEVELOP_CITY_MANAGER,
                        UserRoleService::ROLE_BASE_SUPERVISOR,
                        UserRoleService::ROLE_SPECIAL_0,
                        UserRoleService::ROLE_SPECIAL_1,
                        UserRoleService::ROLE_SPECIAL_2,
                        UserRoleService::ROLE_BASE_WAR_REGION,
                    ])
                ],
                "regionSelectionType" => Rule::when(
                    ($params["role"] ?? null) == UserRoleService::ROLE_BASE_SUPERVISOR,
                    "nullable",
                    self::intInValidateBuild(true, HomeService::REGION_SELECTION_TYPE_TEXT)
                ),
                "region"              => Rule::when(
                    ($params["role"] ?? null) == UserRoleService::ROLE_BASE_SUPERVISOR,
                    "nullable",
                    "required|string|min:1|max:64"
                ),
                "regionValue"         => Rule::when(
                    ($params["role"] ?? null) == UserRoleService::ROLE_BASE_SUPERVISOR,
                    "nullable",
                    "required:string"
                )
            ]
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function shopSelfCheckGroupValidate(array $params)
    {
        return self::getInstance()->validate(
            ["role", "regionSelectionType", "region", "regionValue"],
            $params,
            [
                "role"                => [
                    "required",
                    "int",
                    Rule::in([
                        UserRoleService::ROLE_BASE_PROVINCE,
                        UserRoleService::ROLE_BASE_OBSERVER,
                        UserRoleService::ROLE_BASE_CITY_MANAGER,
                        UserRoleService::ROLE_BASE_DEVELOP_CITY_MANAGER,
                        UserRoleService::ROLE_BASE_SUPERVISOR,
                        UserRoleService::ROLE_SPECIAL_0,
                        UserRoleService::ROLE_SPECIAL_1,
                        UserRoleService::ROLE_SPECIAL_2,
                        UserRoleService::ROLE_BASE_WAR_REGION,
                    ])
                ],
                "regionSelectionType" => Rule::when(
                    ($params["role"] ?? null) == UserRoleService::ROLE_BASE_SUPERVISOR,
                    "nullable",
                    self::intInValidateBuild(true, HomeService::REGION_SELECTION_TYPE_TEXT)
                ),
                "region"              => Rule::when(
                    ($params["role"] ?? null) == UserRoleService::ROLE_BASE_SUPERVISOR,
                    "nullable",
                    "required|string|min:1|max:64"
                ),
                "regionValue"         => Rule::when(
                    ($params["role"] ?? null) == UserRoleService::ROLE_BASE_SUPERVISOR,
                    "nullable",
                    "required:string"
                )
            ]
        );
    }
}
